<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="formInline.user" placeholder="姓名" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selectUserByNameLocal">查询</el-button>
      </el-form-item>
    </el-form>

    <el-dialog title="修改用户信息" :visible.sync="dialogVisible">
      <el-form :model="form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="喜欢的口味">
          <el-select v-model="form.hobby">
            <el-option
              v-for="item in options"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="doUpdate(form)">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column align="center" label="ID" width="95" prop="id" />
      <el-table-column label="用户名" prop="username" width="140" />
      <el-table-column label="姓名" width="140" align="center" prop="name" />
      <el-table-column label="手机号" width="140" align="center" prop="mobile" />
      <el-table-column class-name="status-col" label="喜爱的口味" width="140" align="center" prop="hobby" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="middle"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            size="middle"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getList } from '@/api/table'
import { selectUserByName } from '@/api/table'
import { deleteUser } from '@/api/table'
import { getClassOptions } from '@/api/class'
import { updateUser } from '@/api/table'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogVisible: false,
      formInline: {
        user: '',
        region: ''
      },
      form: {
        name: '',
        username: '',
        password: '',
        phone: '',
        hobby: ''
      },
      options: []
    }
  },
  created() {
    this.fetchData()
    this.getOptions()
  },
  methods: {
    fetchData() {
      const data = {}
      data.page = 0
      data.length = 30
      this.listLoading = true
      getList(data).then(response => {
        this.list = response.data.data
        this.listLoading = false
        console.log(this.list)
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    selectUserByNameLocal() {
      const data = {}
      selectUserByName(data).then(res => {
        this.list = res.data.data()
      })
    },
    handleDelete(data) {
      deleteUser(data).then(res => {
        this.$message = '删除成功'
        this.fetchData()
      })
    },
    handleEdit(data) {
      console.log(data)
      this.form = data
      this.dialogVisible = true
    },
    getOptions() {
      const data = {}
      getClassOptions(data).then(res => {
        console.log(res)
        this.options = res.data
        console.log(this.options)
      })
    },
    doUpdate(data) {
      console.log(data)
      updateUser(data).then((res) => {
        console.log(res)
        if (res.code === 100) {
          this.$message('修改成功')
          this.dialogVisible = false
          this.fetchData(data)
        }
      })
    }
  }
}
</script>
